/**
 * Created by Administrator on 2015/6/20 0020.
 */
define('edit',['avalon','MDEditor'],function(){
    return edit=avalon.define({
        $id:'edit',
        ready:function(id){
            edit.getClass()
            if(id==0){
                //当前为发布
                edit.isPublish=true
            }
            else{
                edit.isPublish=false
                edit.ArticleID=id
                edit.getArticle(id)
            }

            avalon.scan()
        },

        //重置
        reset:function(){
            edit.Title="";
            edit.ClassID="";
            edit.isPublish=true;
            edit.ArticleID=0;
            MDEditor.md=""
        },

        //验证表单合法性
        check:function(){
            function checkTitle(){
                if(edit.ClassID!=""){
                    return true
                }
                else{
                    tip.on("您还没有选择文章的分类")
                    return false
                }
            }
            function checkClass(){
                if(edit.Title!=""){
                    return true
                }
                else{
                    tip.on("您还没有给文章取个名字")
                    return false
                }
            }
            if(checkTitle()&&checkClass()){
                return true
            }

        },

        editor:"./plugins/MDEditor/MDEditor.html",

        show:'block',
        isPublish:true,
        ArticleID:0,
        //发布文章addArticle
        Title:'',
        ClassID:"",
        addArticle:function(){
            if(edit.check()){

                $.call({
                    type:"post",
                    i:"AddArticle",
                    data:{
                        ClassID:edit.ClassID,	//文章分类编号	int		true	1	99999999999
                        MD:MDEditor.md,	//MD文档	string		true	1	99999999999
                        HTML:marked(MDEditor.md),	//HTML格式内容	string		false	1	99999999999
                        //ColumnID	//栏目编号	int		false	1	99999999999	0
                        Title:edit.Title	//标题	char		false	1	50
                        //CoverPID	//封面图片编号	int		false	1	99999999999
                        //Memo	//描述	char		false	1	200
                        //Tab	//文章标签	char		false	1	200
                    },
                    success:function(res){
                        if(res.c==200){
                            tip.on("发布成功",1,3000)
                            window.location.href='#!/art/'+res.d.ArticleID
                            edit.reset()
                        }
                        else{
                            logErr(res)
                        }
                    }

                })
            }
        },


        //标题动效
        lableMT:5,
        lableJump:function(){
            edit.lableMT=-25
        },
        lableBack:function(){
            if(edit.Title==""){

                edit.lableMT=5
            }
        },
        //获取分类
        classList:[],
        getClass:function(){
            $.call({
                type:'post',
                i:'CMS_GetArticleClass',
                data:{
                    ClassID:'All',
                    Page:1,
                    PageNumber:9999999
                },
                success:function(res){
                    if(res.c==200){
                        console.log('分类获取成功')
                        edit.classList=res.d.List
                        edit.lableJump()
                    }
                    else{
                        console.log(res.c+":"+res.m)
                    }
                }

            })
        },

        /*编辑*/
        getArticle:function(id){
            $.call({
                type:"post",
                i:"GetArticle",
                data:{
                    ArticleID:id
                },
                success:function(res){
                    if(res.c==200){
                        edit.Title=res.d.Title
                        MDEditor.md=res.d.MD
                        edit.ClassID=res.d.ClassID
                        MDEditor.trs()
                    }
                }
            })
        },

        //保存标题
        saveTitle:function(){
            if(edit.check()){
                $.call({
                    type:"post",
                    i:"SaveArticle",
                    data:{
                        ArticleID:edit.ArticleID,
                        Title:edit.Title,
                        ClassID:edit.ClassID
                    },
                    success:function(res){
                        if(res.c==200){
                            tip.on("保存成功",1,3000)

                        }
                        else{
                            logErr(res)
                        }
                    }
                })
            }
            edit.saveContent()
        },
        //保存内容
        saveContent:function(){
            $.call({
                type:"post",
                i:"SaveArticleContent",
                data:{
                    ArticleID:edit.ArticleID,
                    MD:MDEditor.md,	//MD文档	string		true	1	99999999999
                    HTML:marked(MDEditor.md)	//HTML格式内容	string		false	1	99999999999
                },
                success:function(res){
                    if(res.c==200){
                        tip.on("内容更新成功",1,3000)
                        window.location.href='#!/art/'+edit.ArticleID
                        edit.reset()
                    }
                    else{
                        logErr(res)
                    }
                }
            })
        },

        $opt:{
            height:600,
            loadLocaDoc:false,
        }



    })
})